package com.chinacache.maint.commons.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;

/**
 * 该类的功能是从HttpSession中读写数据。
 * 
 * @author stone.zhang
 * 
 */
@SuppressWarnings("unchecked")
public class HttpUtil {

	/**
	 * 从HttpSession中读取一个制定名字的值，如果值不存在，则返回null。
	 * 
	 * @param key
	 * @return
	 */
	public static Object getFromSession(Object key) {
		if (ActionContext.getContext().getSession() != null)
			return ActionContext.getContext().getSession().get(key);
		else
			return null;
	}

	/**
	 * 往HttpSession中写入一个名值对。
	 * 
	 * @param key
	 * @param value
	 */

	public static void putToSession(Object key, Object value) {
		ActionContext.getContext().getSession().put(key, value);
	}

	public static HttpServletRequest getHttpServletRequest() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(StrutsStatics.HTTP_REQUEST);
		return request;
	}
	
	public static HttpServletResponse getHttpServletRespone() { 
		HttpServletResponse response = (HttpServletResponse) ActionContext
		.getContext()
		.get(StrutsStatics.HTTP_RESPONSE);
		return response;
	}
}
